import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Box 			= $Lazy.load(()=>import('#tp/box/box'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const message 		= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:{},
		detailData:{},
		isEdit:false,	// 控制表单是否可编辑
		editOrAudit: '',	// 编辑 或 审核
		moreEdit: false, 	// 控制是否批量编辑
		customHide:true,	// 控制自定义属性显示或隐藏
		formData: [
			{ label:'条形码', 		name:'spec_code', 			full:true,	disabled:true,	required:true },
			/* 病人详细信息添加字段显示第三方条码 2021-08-16  --耿琰 */
			{ label: '第三方条码', 	name: 'third_spec_code', 	full:true },
			{ label:'送检医院', 		name:'hosp_name', 			full:true,	disabled:true, 		},
			{ label:'姓名', 		name:'patient_name', 		half:true,	required:true,	itemClassName:'patient_name'},
			{ label:'性别', 		name:'sex',					half:true, 	type:'select',	idStr: 'code', 	value:'45703',	isKeydown: 'sex',	 onValue: ({rows})=>{if(rows)this.query.sex_name = rows.name}},
			{ label:'年龄一', 		name:'age_first_val', 		half:true},
			{ label:'年龄类型一', 	name:'age_first_type', 		half:true, 	type:'select',	idStr: 'code',	value: "30001",	onValue: ({rows})=>{if(rows)this.query.age_first_type_name = rows.name}},
			{ label:'年龄二', 		name:'age_second_val', 		half:true},
			{ label:'年龄类型二', 	name:'age_second_type', 	half:true, 	type:'select',	idStr: 'code', 	value: "30003",	onValue: ({rows})=>{if(rows)this.query.age_second_type_name = rows.name}},
			{ label:'送检科室', 		name:'department_name', 	half:true},
			{ label:'床号', 		name:'bed_no', 				half:true},
			{ label:'门诊/住院号', 	name:'outpatient', 			half:true},
			{ label:'送检医生', 		name:'doctor', 				half:true},
			{ label:'医生电话', 		name:'doctor_phone', 		half:true},
			{ label:'受检人电话', 	name:'patient_phone', 		half:true},
			{ label:'物流人员', 		name:'real_name', 			half:true,	disabled:true},
			{ label:'受检人类型', 	name:'case_type', 			half:true,	type:'select',	idStr: 'code',	isKeydown: 'case_type' },
			{ label:'标本性状', 		name:'spec_traits', 		half:true,	type:'select',	idStr: 'code',	onValue: ({rows})=>{if(rows)this.query.spec_traits_name = rows.name}},
			{ label:'采集类型',		name: 'collect_type', 		half:true, 	type: 'radio', 	mode:'button'},
			{ label:'医院标识', 		name:'hosp_identification', full:true},
			{ label:'证件类型', 		name:'card_type', 			full:true, 	type:'select',	value: '87007', idStr: 'code' },
			{ label:'证件号', 		name:'card_no', 			full:true },
			{ label:'采样日期',		name:'samp_date',  			half:true, 	type:'date-time', 	format:1,	submit:1,	showToday:false,	onValue:({value,formData})=>{
				if(value){
					$fn.disabledRows(formData, 'samp_time', false)
					this.setState({ formData })
				}
			} },
			{ label:'采样时间',		name:'samp_time',			half:true, 	type:'time' },
			{ label:'病理固定时间',	name:'bingli_coll_time',	full:true, 	type:'date-time',	format:2 },
			{ label:'病理离体时间',	name:'bingli_take_time',	full:true, 	type:'date-time',	format:2 },
			{ label:'临床诊断', 		name:'diagnosis_info', 		full:true,	type:'textarea'},
			{ label:'识别标签',		name: 'tags', 				full:true, 	type: 'checkboxGroup', idStr:'code' },
			{ label:'备注', 		name:'remark', 				full:true,	type:'textarea'},
			// 病人基本信息 增加5个自定义字段 2021-10-15  --马婷 
			{ hide:true,	custom:true, onClick:()=>{this.customClick()}},
			{ label:'备用1', 		name:'user_defined1', 				full:true,	hide:true },
			{ label:'备用2', 		name:'user_defined2', 				full:true,	hide:true },
			{ label:'备用3', 		name:'user_defined3', 				full:true,	hide:true },
			{ label:'备用4', 		name:'user_defined4', 				full:true, 	hide:true },
			{ label:'备用5', 		name:'user_defined5', 				full:true, 	hide:true },
			{ label:'备用6', 		name:'user_defined6', 				full:true, 	hide:true },
			{ label:'备用7', 		name:'user_defined7', 				full:true, 	hide:true },
			{ label:'备用8', 		name:'user_defined8', 				full:true, 	hide:true },
			{ label:'备用9', 		name:'user_defined9', 				full:true, 	hide:true },
			{ label:'备用10', 		name:'user_defined10', 				full:true, 	hide:true },
		],
		
	}
	query = {page_source: 'sp'}
	btn_power = { keep:1, audit:1 }
	componentDidMount(){
		// 按钮添加权限 2022-07-05 17:15:24  --马婷
		const { ban_btn } = $fn.local('permissions')
		if($fn.hasArray(ban_btn)){
			if(ban_btn.includes('speccaseinfo.keep')){		// 编辑
				this.btn_power.keep = 0
			}
			if(ban_btn.includes('speccaseinfo.audit')){		// 审核
				this.btn_power.audit = 0
			}
		}
		
		this.getSelect()
	}
	
	// 点击展开或关闭自定义属性
	customClick = () => {
		const { customHide, formData } = this.state
		this.setState({customHide:!customHide},()=>{
			$fn.hideRows(formData,['user_defined1','user_defined2','user_defined3','user_defined4','user_defined5','user_defined6','user_defined7','user_defined8','user_defined9','user_defined10'], this.state.customHide)
			this.setState({formData})
		})
	}
	
	setValue = (uuid, edit) => {	
		this.query = {page_source: 'sp'}
		const { formData } = this.state
		$http.submit(this,'spec-case-info/info',{ param:{uuid}, loading:false }).then(data=>{
			// 采样日期不能大于接收日期
			const sampDate = $fn.getRow( formData, 'samp_date')
			const { check_time } = data
			if( check_time ){
				const check = check_time.split(' ')
				sampDate.defaultPickerValue = check[0]
				sampDate.after = check[0]
			}
			this.formRef.reset()
			if(!data.samp_date){
				$fn.disabledRows(formData, 'samp_time', true)
			}else{
				$fn.disabledRows(formData, 'samp_time', false)
			}
			this.formRef.setValue(data)
			this.setState({ formData, detailData:data, isEdit:false})
			if(this.state.moreEdit || edit){
				this.controls()[3].onClick()
				if(data.img_url) {
					this.props.previewImage(data.img_url)
				}else {
					this.props.previewImage('')
					this.props.previewImageClose()
				}
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			const dic = await f.getDicCache({ code: [ 45700, 21000, 42000, 69700, 30000, 87000 ]})
			// 标本性状
			$fn.setRow( formData, 'spec_traits', dic['21000'] )
			// 受检人类型
			$fn.setRow( formData, 'case_type', dic['42000'] )
			// 性别
			$fn.setRow( formData, 'sex', dic['45700'] )
			//识别标签
			$fn.setRow( formData, 'tags', dic['69700'] )
			// 年龄类型
			$fn.setRow( formData, 'age_first_type', dic['30000'] )
			$fn.setRow( formData, 'age_second_type', dic['30000'] )
			// 采集类型
			$fn.setRow( formData, 'collect_type', f.collect_type )
			// 身份证类型 [2021-8-17 3:19:24 ?F10: PM?] --伍智睿
			$fn.setRow( formData, 'card_type', dic['87000'])

			this.setState({ formData })
		})
	}
	
	// 病人详细信息按钮
	controls = () => {
		const { detailData, isEdit} = this.state	
		const { keep, audit } = this.btn_power
		// 保存 查看图片 审核 编辑
		return [
			Controls.click.call(this, '保存', ()=>{
				this.setState({editOrAudit:true})
				const formValue = this.formRef.getValue()
				if(!formValue.samp_date ) formValue.samp_date = ''
				if(!formValue.samp_time ) formValue.samp_time = ''
				formValue.coll_time = formValue.samp_date + ' '+ formValue.samp_time
				this.submit = { entry_type: 'main', ...detailData, ...this.query,...formValue }
				this.formRef.submit()
			},{hide:!isEdit, code: 'F2'} ),
			Controls.click.call(this,'查看图片',()=> {this.props.previewImage(detailData.img_url)},{disabled:!detailData.img_url}),
			audit && Controls.click.call(this, '审核', ()=>{											// 审核
				this.setState({editOrAudit:false})
				const formValue = this.formRef.getValue()
				if(!formValue.samp_date ) formValue.samp_date = ''
				if(!formValue.samp_time ) formValue.samp_time = ''
				formValue.coll_time = formValue.samp_date + ' '+ formValue.samp_time
				this.submit = { entry_type: 'main', ...detailData, ...this.query,...formValue  }
				this.formRef.submit()
				
			},{hide:detailData ? !+detailData.status:true, code: 'F3'} ),
			keep && Controls.click.call(this, '编辑', ()=>{											// 审核
				const {spec_code,uuid} = this.state.detailData
				$http.submit(null,'spec-case-info/checkEditEnabled',{ param: {spec_code,uuid}}).then(({editEnabled})=>{
					if(+editEnabled === 1){
						this.setState({isEdit: 1 })
						message.then(f=>f.success(`请求成功`))
						document.querySelector('.patient_name .ant-input.ant-input-borderless').focus()
					}else{
						message.then(f=>f.error(`请前往双录入页面进行修改`))
					}
				})
			},{hide:isEdit, code: 'F1'} ),			
			keep && { label:'批量编辑', type:'checkbox', onChange:(value)=>{
				if(value)document.querySelector('.patient_name .ant-input.ant-input-borderless').focus()
				this.setState({moreEdit:value})
			} }
		]
		
	}
	
	render(){
		const { isEdit, editOrAudit, submitLoading, customHide } = this.state
		return (
			<>
				<Box title='病人详细信息' small full style={{height:'100%'}} scroll flex loading={submitLoading} controls={this.controls()}>
					{
						SubmitForm.call(this,{ disabled:!isEdit, labelWidth:'s7', noholder:true,customHide,
							api: editOrAudit?'spec-case-info/keep':'spec-case-info/pass', 
							onRefresh:()=>{
								this.props.onNext(editOrAudit)
							}
						} ) 
					}
				</Box>
			</>
		)
	}
}

export default Index